package com.scm.controller;

import com.scm.pojo.PayRecord;
import com.scm.pojo.PoMain;
import com.scm.pojo.SoMain;
import com.scm.pojo.Stock;
import com.scm.service.*;
import com.scm.util.Page;
import com.scm.util.QueryResult;
import com.scm.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName MonthFormController
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/19 11:16
 * @Version 1.0
 **/
@RestController
public class MonthFormController {
    @Autowired
    private SoMainService soMainService;

    @Autowired
    private Util util;

    @Autowired
    private PoMainService poMainService;

    @Autowired
    private StockService stockService;

    @Autowired
    private ScmService scmService;
    @Autowired
    private ProductService productService;

    @Autowired
    private PayRecordService payRecordService;

    @RequestMapping("getMonthSoMain")
    public QueryResult getMonthSoMain(Integer currentPage, Integer pageSize,String date){
        Map map = Page.get(currentPage,pageSize);
        map.put("date",date);
        List<SoMain> list=soMainService.getMonthSoMain(map);
        list = util.showSoMain(list);

        List<SoMain> list1 =soMainService.getCountMonthSoMain(map);
        int total = list1.size();
        double allPrice=0;
        int overCount=0;
        double overPrice=0;
        for (SoMain soMain:list1){
            allPrice+=Double.parseDouble(soMain.getPoTotal());
            if (soMain.getStatus().equals("5")){
                overCount++;
                overPrice+=Double.parseDouble(soMain.getPoTotal());
            }
             else  if (soMain.getStatus().equals("4")){
                overPrice+=Double.parseDouble(soMain.getPoTotal());
            }else if (soMain.getStatus().equals("3")&&soMain.getPayType().equals("款到发货")){
                 overPrice+=Double.parseDouble(soMain.getPoTotal());
            }

        }
        return QueryResult.ok2(total,list,overCount,allPrice,overPrice);

    }

    @RequestMapping("getMonthPoMain")
    public QueryResult getMonthPoMain(Integer currentPage, Integer pageSize,String date){
        Map map =Page.get(currentPage,pageSize);
        map.put("date",date);
        List<PoMain> list = poMainService.getMonthPoMain(map);
        list = util.show(list);
        List<PoMain> list1 = poMainService.getAllMonthPoMain(date);
        int total = list1.size();
        double allPrice=0;
        double overPrice = 0;
        int overCount = 0;
        for (PoMain poMain:list1){
            allPrice+=Double.parseDouble(poMain.getPoTotal());
            if (poMain.getStatus().equals("5")){
                overCount++;
                overPrice+=Double.parseDouble(poMain.getPoTotal());
            }
            else  if (poMain.getStatus().equals("4")){
                overPrice+=Double.parseDouble(poMain.getPoTotal());
            }else if (poMain.getStatus().equals("3")&&poMain.getPayType().equals("款到发货")){
                overPrice+=Double.parseDouble(poMain.getPoTotal());
            }
        }
        return QueryResult.ok2(total,list,overCount,allPrice,overPrice);
    }

    @RequestMapping("outMonthSoMain")
    public QueryResult outMonthSoMain(Integer currentPage, Integer pageSize,String date,String stockType){
        Map<String, Object> map = Page.get(currentPage, pageSize);
        map.put("date",date);
        map.put("stockType",stockType);
        List<Stock> stocks = stockService.outMonthSoMain(map);
        for (Stock stock:stocks){
            stock.setCreateUser(scmService.getUserName(stock.getCreateUser()));
            String name = productService.getProductName(stock.getProductCode());
            stock.setProductCode(name);

        }

        int total = stockService.outAllMonthSoMain(map);
        return QueryResult.ok(total,stocks);

    }
    @RequestMapping("payMonth")
    public QueryResult payMonth(Integer currentPage, Integer pageSize,String date){
        Map<String,Object> map =Page.get(currentPage,pageSize);
        map.put("date",date);
        List<PayRecord> list = payRecordService.payMonth(map);
        list = payRecordService.showPayRecord(list);
        List<PayRecord> payRecords = payRecordService.countPayMonth(date);
        int total=payRecords.size();
        int allPrice=0;
        int overPrice=0;
        for (PayRecord p :payRecords){
            if (p.getPay_type().equals("1")){
                allPrice+=Double.parseDouble(p.getPay_price());
            }else if (p.getPay_type().equals("-1")){
                overPrice+=Double.parseDouble(p.getPay_price());
            }
        }
        return QueryResult.ok2(total,list,0,allPrice,overPrice);


    }
}
